package main

import (
	"fmt"
	"runtime"
	"sync"
	"time"
)

var wg sync.WaitGroup
var ch = make(chan int64, 1)

func inc() {
	defer wg.Done()
	for i := 0; i < 100000; i++ {
		t := <-ch
		t++
		ch <- t
	}
}

func main() {
	start := time.Now()
	ch <- 0
	wg.Add(5)
	for i := 0; i < 5; i++ {
		go inc()
	}
	fmt.Printf("GO协程数: %d\n", runtime.NumGoroutine())
	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

	wg.Wait()
	fmt.Printf("执行时长：%d 微妙:\n", time.Since(start).Microseconds())
	fmt.Printf("GO协程数: %d\n", runtime.NumGoroutine())
	fmt.Printf("count: %d\n", <-ch)
}
